package com.css.zfzx.fxpg.modules.rescue.repository.impl;

import com.css.zfzx.fxpg.modules.rescue.repository.FxRescueNativeRepository;
import com.css.zfzx.fxpg.modules.rescue.repository.entity.FxRescueEntity;
import com.css.zfzx.fxpg.modules.rescue.viewobjects.FxRescueQueryParams;
import com.css.bpm.platform.utils.PlatformObjectUtils;
import org.slf4j.Logger;
import org.slf4j.LoggerFactory;
import org.springframework.data.domain.Page;
import org.springframework.data.domain.PageImpl;
import org.springframework.data.domain.PageRequest;
import org.springframework.data.domain.Pageable;
import org.springframework.stereotype.Repository;

import javax.persistence.EntityManager;
import javax.persistence.PersistenceContext;
import javax.persistence.Query;
import java.math.BigInteger;
import java.text.SimpleDateFormat;
import java.util.Date;
import java.util.List;

/**
 * @author leon
 * @date 2020-12-02
 */
@Repository
public class FxRescueNativeRepositoryImpl implements FxRescueNativeRepository {
    @PersistenceContext
    private EntityManager em;

    private static final Logger log = LoggerFactory.getLogger(FxRescueNativeRepositoryImpl.class);


    @Override
    public Page<FxRescueEntity> queryFxRescues(FxRescueQueryParams queryParams, int curPage, int pageSize) {
        StringBuilder sql = new StringBuilder("select * from fx_rescue ");
        StringBuilder whereSql = new StringBuilder(" where 1=1 ");
        if (!PlatformObjectUtils.isEmpty(queryParams.getProvince())) {
            whereSql.append(" and province = :province");
        }
        if (!PlatformObjectUtils.isEmpty(queryParams.getCityCountyName())) {
            whereSql.append(" and city_county_name like :cityCountyName");
        }
        if (!PlatformObjectUtils.isEmpty(queryParams.getSurveySpotName())) {
            whereSql.append(" and survey_spot_name = :surveySpotName");
        }
        Query query = this.em.createNativeQuery(sql.append(whereSql).toString(), FxRescueEntity.class);
        StringBuilder countSql = new StringBuilder("select count(*) from fx_rescue ");
        Query countQuery = this.em.createNativeQuery(countSql.append(whereSql).toString());
        if (!PlatformObjectUtils.isEmpty(queryParams.getProvince())) {
            query.setParameter("province", queryParams.getProvince());
            countQuery.setParameter("province", queryParams.getProvince());
        }
        if (!PlatformObjectUtils.isEmpty(queryParams.getCityCountyName())) {
            query.setParameter("cityCountyName", queryParams.getCityCountyName());
            countQuery.setParameter("cityCountyName", queryParams.getCityCountyName());
        }
        if (!PlatformObjectUtils.isEmpty(queryParams.getSurveySpotName())) {
            query.setParameter("surveySpotName", queryParams.getSurveySpotName());
            countQuery.setParameter("surveySpotName", queryParams.getSurveySpotName());
        }
        Pageable pageable = PageRequest.of(curPage - 1, pageSize);
        query.setFirstResult(pageable.getPageNumber() * pageable.getPageSize());
        query.setMaxResults(pageable.getPageSize());
        List<FxRescueEntity> list = query.getResultList();
        BigInteger count = (BigInteger) countQuery.getSingleResult();
        Page<FxRescueEntity> fxRescuePage = new PageImpl<>(list, pageable, count.longValue());
        return fxRescuePage;
    }

    @Override
    public List<FxRescueEntity> listFxRescues(FxRescueQueryParams queryParams) {
        StringBuilder sql = new StringBuilder("select * from fx_rescue ");
        StringBuilder whereSql = new StringBuilder(" where 1=1 ");
        if (!PlatformObjectUtils.isEmpty(queryParams.getProvince())) {
            whereSql.append(" and province = :province");
        }
        if (!PlatformObjectUtils.isEmpty(queryParams.getCity())) {
            whereSql.append(" and city like :city");
        }
        if (!PlatformObjectUtils.isEmpty(queryParams.getArea())) {
            whereSql.append(" and area like :area");
        }
        if (!PlatformObjectUtils.isEmpty(queryParams.getCityCountyName())) {
            whereSql.append(" and city_county_name = :cityCountyName");
        }
        if (!PlatformObjectUtils.isEmpty(queryParams.getSurveySpotName())) {
            whereSql.append(" and survey_spot_name = :surveySpotName");
        }
        Query query = this.em.createNativeQuery(sql.append(whereSql).toString(), FxRescueEntity.class);
        if (!PlatformObjectUtils.isEmpty(queryParams.getProvince())) {
            query.setParameter("province", queryParams.getProvince());
        }
        if (!PlatformObjectUtils.isEmpty(queryParams.getCity())) {
            query.setParameter("city", "%" + queryParams.getCity() + "%");
        }
        if (!PlatformObjectUtils.isEmpty(queryParams.getArea())) {
            query.setParameter("area", "%" + queryParams.getArea() + "%");
        }
        if (!PlatformObjectUtils.isEmpty(queryParams.getCityCountyName())) {
            query.setParameter("cityCountyName", queryParams.getCityCountyName());
        }
        if (!PlatformObjectUtils.isEmpty(queryParams.getSurveySpotName())) {
            query.setParameter("surveySpotName", queryParams.getSurveySpotName());
        }
        return query.getResultList();
    }
}
